/**
 * Created by Wang on 2017-06-18.
 */
import {AuthContract} from "../cantracts/AuthContract";
import {Http} from "@angular/http";
import {Observable} from "rxjs/Observable";

export class Auth implements AuthContract {
  apiUrl = '';
  userDataKey = 'user_info';
  usernameKey = 'username';
  emailKey = 'email';
  apiTokenKey = 'api_token';
  http: Http;


  check(): boolean {
    if (localStorage.getItem(this.usernameKey) != null
      && localStorage.getItem(this.emailKey) != null
      && localStorage.getItem(this.apiTokenKey) != null
      && localStorage.getItem(this.userDataKey) != null
    )
      return true;
    else {
      // this.logout();
      return false;
    }
  }

  login(username: string, password: string): Observable<any> {
    this.logout();
    let link = this.apiUrl + '/login/' + username + '/' + password;
    return this.http.get(link)
      .map(response => {
        let res = response.json();
        if (res.success)
          this.saveUser(res.data);
        return res;
      });
  }

  logout() {
    localStorage.clear();
  }

  userInfo(): any | null {
    if (this.check())
      return localStorage.getItem(this.userDataKey);
  }

  apiToken(user?: any): string | null {
    if (user != null) {
      return user.api_token;
    }
    if (this.check())
      return localStorage.getItem(this.apiTokenKey);
  }

  username(user?: any): string | null {
    if (user != null) {
      return user.user_id;
    }
    if (this.check())
      return localStorage.getItem(this.usernameKey);
  }

  email(user?: any): string | null {
    if (user != null) {
      return user.email;
    }
    if (this.check())
      return localStorage.getItem(this.emailKey);
  }


  register(data: any): Observable<any> {
    let link = this.apiUrl + '/register';
    console.log(JSON.stringify(data));
    return this.http.post(link, JSON.stringify(data))
      .map(response => {
        let res = response.json();
        if (res.success)
          this.saveUser(res.data);
        return res;
      });
  }

  retrievePassword(loginId: string, email: string): Observable<any> {
    return null;
  }

  changePassword(oldPassword: string, newPassword: string): Observable<any> {
    let link = this.apiUrl +
      "/reset_password?old_password=" + oldPassword +
      "&new_password=" + newPassword +
      "&email=" + this.email() +
      "&token=" + this.apiToken();
    return this.http.get(link).map(response => {
      let res = response.json();
      if (res.success)
        this.saveUser(res.data);
      return res;
    });

  }

  update(user: any): Observable<any> {
    let link = this.apiUrl + "/update_user";
    return this.http.post(link, JSON.stringify(user)).map(
      response => {
        let res = response.json();
        if (res.success)
          this.saveUser(res.data);
        return res;
      }
    );

  }

  protected saveUser(user: any) {
    localStorage.setItem(this.emailKey, this.email(user));
    localStorage.setItem(this.apiTokenKey, this.apiToken(user));
    localStorage.setItem(this.usernameKey, this.username(user));
    localStorage.setItem(this.userDataKey, JSON.stringify(user));
  }

  userId(user?): string | null {
    if (user != null)
      return user.Customer_ID;
    if (this.check()) {
      let info = this.userInfo();
      return info.Customer_ID;
    }
  }
  employeeId(user?) {
    if (user != null)
      return user.Employee_ID;
    if (this.check()) {
      let info = this.userInfo();
      return info.Employee_ID;
    }
  }

}
